package cn.lger.service;

import cn.lger.dao.CategoryDao;
import cn.lger.pojo.Category;
import cn.lger.pojo.Page;
import cn.lger.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Code that Changed the World
 *
 * @author Pro
 * @date 2018-06-17.
 */

@Service
public class CategoryService {

    @Resource
    private CategoryDao categoryDao;

    public List<Category> findAll(){
        return categoryDao.findAll();
    }

    @Transactional(rollbackFor = Exception.class)
    public Category insertOne(Category category) {
        if (categoryDao.insertOne(category)>0){
            return category;
        }else {
            return null;
        }
    }

    public int deleteOneById(int id) {
        return categoryDao.deleteOneById(id);
    }

    public Page<Category> findOnePage(Integer currentPage, Integer size) {
        Page<Category> page = PageUtil.generator(currentPage, size);
        page.setContent(categoryDao.findOnePage(page.getPage(), page.getSize()));
        page.setCount(categoryDao.getTotal());
        return page;
    }
}
